package com.example.perplonews.a_a_mvp.model.models

import BaseModel
import com.example.perplonews.a_a_mvp.contract.News_Contract
import com.example.perplonews.a_a_mvp.model.HttpCode
import com.example.perplonews.a_b_entity.*
import com.example.perplonews.a_b_utils.LjlUtils
import com.example.perplonews.a_c_network.Api
import com.example.perplonews.a_c_network.HttpFactory
import com.example.perplonews.a_c_network.HttpType
import com.example.perplonews.a_d_fragment.b_perplo.entity.UserLoveEntity
import com.example.perplonews.a_d_fragment.c_video.entity.VideoNewsEntity
import io.reactivex.Observable
import io.reactivex.functions.Function


class News_Model : BaseModel(), News_Contract.NewsModel {
    override fun requestAll(list: ArrayList<HashMap<String, Any>>): Observable<BaseEntity> {
        var obs = ArrayList<Observable<BaseEntity>>()

        var i = 0
        while (i < list.size) {
            var hashMap = list[i]
            var code = hashMap.get("code")
            hashMap.remove("code")
            when (code) {

                HttpCode.USERCHANNELCODE -> {
                    var map = HttpFactory.instance.factory(HttpType.TOKEN)
                        ?.getRetrofit()
                        ?.create(Api::class.java)
                        ?.requestUserChannels(createBody(hashMap)!!)
                        ?.map(object : Function<ChannelEntity, BaseEntity> {
                            override fun apply(t: ChannelEntity): BaseEntity {

                                return t
                            }
                        })
                    obs.add(map!!)
                }
                HttpCode.TEXTBANNERCODE -> {
                    var map = HttpFactory.instance.factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestTextBanner()
                        ?.map(object : Function<TextBannerEntity, BaseEntity> {
                            override fun apply(t: TextBannerEntity): BaseEntity {
                                return t
                            }

                        })
                    obs.add(map!!)
                }
                HttpCode.MORECHANNELCODE -> {
                    var map = HttpFactory.instance.factory(HttpType.TOKEN)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestMoreChannels(createBody(hashMap)!!)
                        ?.map(object : Function<MoreChannel, BaseEntity> {
                            override fun apply(t: MoreChannel): BaseEntity {
                                return t
                            }

                        })
                    obs.add(map)
                }
                HttpCode.IMAGERBANNERCODE -> {
                    var map = HttpFactory.instance.factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestImageBanner()
                        .map(object : Function<ImageBannerEntity, BaseEntity> {
                            override fun apply(t: ImageBannerEntity): BaseEntity {

                                return t
                            }

                        })
                    obs.add(map)
                }
                HttpCode.VIDEONEWSCODE -> {
                    var map = HttpFactory.instance.factory(HttpType.TOKEN)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestVideoNews(createBody(hashMap))
                        .map(object : Function<VideoNewsEntity, BaseEntity> {
                            override fun apply(t: VideoNewsEntity): BaseEntity {
                                LjlUtils.Ljl_Loge("------------------------" + t.values)
                                return t
                            }
                        })
                    obs.add(map)
                }
                HttpCode.USERFOLLOWCODE -> {
                    var map = HttpFactory.instance.factory(HttpType.TOKEN)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestUserFollow(createBody(hashMap)!!)
                        .map(object : Function<UserLoveEntity, BaseEntity> {
                            override fun apply(t: UserLoveEntity): BaseEntity {
                                return t
                            }

                        })
                    obs.add(map)
                }
                HttpCode.FOLLOWNEWSCODE ->{
                    var map = HttpFactory.instance.factory(HttpType.TOKEN)
                        .getRetrofit()
                        .create(Api::class.java)
                        .requestFollowNews(createBody(hashMap)!!)
                        .map(object : Function<FollowAntivity, BaseEntity> {
                            override fun apply(t: FollowAntivity): BaseEntity {
                                return t
                            }

                        })
                    obs.add(map)
                }


            }
            i++
        }

        return Observable.merge(obs)
    }


}